package com.ruoyi.web.controller.system;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.system.domain.ShippingAddress;
import com.ruoyi.system.service.ShippingAddressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @Author HP
 * @Date 2021/3/9
 * @Description TODO
 */
@RestController
@RequestMapping("shipping/address")
public class ShippingAddressController {
    @Autowired
    private ShippingAddressService shippingAddressService;

    @RequestMapping("/queryById")
    public AjaxResult queryById(String user_id){
        List<ShippingAddress> shippingAddress = shippingAddressService.queryById(user_id);

        if (shippingAddress!=null){

            return AjaxResult.success(shippingAddress);

        }
        return AjaxResult.error();
    }

    /**
     * 添加地址
     * @param shippingAddress
     * @return
     */
    @PostMapping("/saveShippingAddress")
    public AjaxResult saveShippingAddress(ShippingAddress shippingAddress){
        String address1 = shippingAddress.getAddress();
        String address2 = shippingAddress.getAddress1();
        String address=address1+"/"+address2;
        shippingAddress.setAddress(address);
        int key = shippingAddressService.saveShippingAddress(shippingAddress);
        return AjaxResult.success(shippingAddress.getId());
    }

    /**
     *  删除地址
     * @param shippingAddress
     * @return
     */
    @PostMapping("/deleteShippingAddress")
    public AjaxResult deleteShippingAddress(int  addressId){
        int  resInt = 0;

        if(addressId >0){
            resInt = shippingAddressService.deleteAddress(addressId);
        }

        if (resInt>0){
            return AjaxResult.success();
        }
            return AjaxResult.error();
    }
}
